package cn.hncu.booksManagementSystem;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.book.factory.BookModuleFactory;
import cn.hncu.booksManagementSystem.user.factory.UserModuleFactory;
import cn.hncu.booksManagementSystem.user.ui.LoginPanel;
import cn.hncu.booksManagementSystem.user.vo.UserModel;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;

/**
 * https://blog.csdn.net/qq_34928644/article/details/80097939 宋
 * https://blog.csdn.net/qq_36741208/article/details/87208689 名
 * https://blog.csdn.net/wangloveall/article/details/7248739 分包
 * admin
 * 123
 */
public class BMS_APP {

	private JFrame mainFrame;
	public static UserModel activeUser = null;
	private static JMenu mnuModule;
	private static JMenuItem mimUserManagement;
	private static JMenuItem mimBooksManagement;
	private static JMenuItem mimInManagement;
	private static JMenuItem mimOutManagement;
	private static JMenuItem mimStockManagement;
	private JMenuItem mimLogOff;

	/**
	 * 设置模块下的所有菜单项都不可点击
	 */
	public static void setAllModuleToFalse() {
		mimUserManagement.setEnabled(false);
		mimBooksManagement.setEnabled(false);
		mimInManagement.setEnabled(false);
		mimOutManagement.setEnabled(false);
		mimStockManagement.setEnabled(false);
	}
	/**
	 * 通过当前用户设置模块下的所有菜单项
	 */
	public static void setMenuItemAllByActiveUser() {
		setAllModuleToFalse();
		if (activeUser!=null) {
			switch (activeUser.getType()) {
			case 1:
				mimUserManagement.setEnabled(true);
				mimBooksManagement.setEnabled(true);
				mimInManagement.setEnabled(true);
				mimOutManagement.setEnabled(true);
				mimStockManagement.setEnabled(true);
				break;
			case 2:
				mimBooksManagement.setEnabled(true);
				break;
			case 3:
				mimInManagement.setEnabled(true);
				break;
			case 4:
				mimOutManagement.setEnabled(true);
				break;
			case 5:
				mimStockManagement.setEnabled(true);
			}
		}
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BMS_APP window = new BMS_APP();
					window.mainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public BMS_APP() {
		initialize();
		mainFrame.setContentPane(new LoginPanel(mainFrame));
		mainFrame.validate();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		mainFrame = new JFrame();
		mainFrame.setResizable(false);
		mainFrame.setTitle("书店管理系统");
		mainFrame.setMinimumSize(new Dimension(800, 600));
		mainFrame.setBounds(300, 100, 450, 300);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		mainFrame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("文件");
		menuBar.add(mnFile);
		
		JMenu mnEdit = new JMenu("编辑");
		menuBar.add(mnEdit);
		
		mnuModule = new JMenu("模块");
		mnuModule.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 24));
		menuBar.add(mnuModule);
		
		mimUserManagement = new JMenuItem("用户管理");
		mimUserManagement.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
		mimUserManagement.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mimUserManagementActionPerformed(e);
			}
		});
		mimUserManagement.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
		mimUserManagement.setHorizontalAlignment(SwingConstants.LEFT);
		mnuModule.add(mimUserManagement);
		
		mimBooksManagement = new JMenuItem("图书管理");
		mimBooksManagement.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK));
		mimBooksManagement.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mimBooksManagementActionPerformed(e);
			}
		});
		mimBooksManagement.setHorizontalAlignment(SwingConstants.CENTER);
		mimBooksManagement.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
		mnuModule.add(mimBooksManagement);
		
		mimInManagement = new JMenuItem("进货管理");
		mimInManagement.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mimInManagementActionPerformed(e);
			}
		});
		mimInManagement.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));
		mimInManagement.setHorizontalAlignment(SwingConstants.CENTER);
		mimInManagement.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
		mnuModule.add(mimInManagement);
		
		mimOutManagement = new JMenuItem("销售管理");
		mimOutManagement.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mimOutManagementActionPerformed(e);
			}
		});
		mimOutManagement.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
		mimOutManagement.setHorizontalAlignment(SwingConstants.CENTER);
		mimOutManagement.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
		mnuModule.add(mimOutManagement);
		
		mimStockManagement = new JMenuItem("库存管理");
		mimStockManagement.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mimStockManagementActionPerformed(e);
			}
		});
		mimStockManagement.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		mimStockManagement.setHorizontalAlignment(SwingConstants.CENTER);
		mimStockManagement.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
		mnuModule.add(mimStockManagement);
		
		mimLogOff = new JMenuItem("注销");
		mimLogOff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mimLogOffActionPerformed(e);
			}
		});
		mimLogOff.setHorizontalAlignment(SwingConstants.CENTER);
		mimLogOff.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 16));
		mnuModule.add(mimLogOff);
		//设置模块下的所有菜单项都不可选
		setAllModuleToFalse();
		
		JMenu mnHelp = new JMenu("帮助");
		menuBar.add(mnHelp);
		mainFrame.getContentPane().setLayout(null);
		
		
	}
	/**
	 * 库存模块事件响应,通过类全名来创建对象
	 * @param e 事件源
	 */
	protected void mimStockManagementActionPerformed(ActionEvent e) {
		mainFrame.setContentPane(new cn.hncu.booksManagementSystem.stock.ui.ListPanel(mainFrame));
		mainFrame.validate();
	}
	/**
	 * 销售模块事件响应,通过类全名来创建对象
	 * @param e 事件源
	 */
	protected void mimOutManagementActionPerformed(ActionEvent e) {
		//初始化col和idqm
		cn.hncu.booksManagementSystem.out.ui.ListPanel.initStaticAttribute();
		
		mainFrame.setContentPane(new cn.hncu.booksManagementSystem.out.ui.ListPanel(mainFrame));
		mainFrame.validate();
	}
	/**
	 * 进货模块事件响应,通过类全名来创建对象
	 * @param e 事件源
	 */
	protected void mimInManagementActionPerformed(ActionEvent e) {
		//初始化col和idqm
		cn.hncu.booksManagementSystem.in.ui.ListPanel.initStaticAttribute();
		
		mainFrame.setContentPane(new cn.hncu.booksManagementSystem.in.ui.ListPanel(mainFrame));
		mainFrame.validate();
	}
	/**
	 * 图书模块事件响应,通过工厂方法
	 * @param e 事件源
	 */
	protected void mimBooksManagementActionPerformed(ActionEvent e) {
		mainFrame.setContentPane(BookModuleFactory.getBookModuleListPanel(mainFrame));
		mainFrame.validate();
	}
	/**
	 *	用户模块事件响应,通过工厂方法
	 * @param e 事件源
	 */
	protected void mimUserManagementActionPerformed(ActionEvent e) {
		mainFrame.setContentPane(UserModuleFactory.getUserModuleListPanel(mainFrame));
		mainFrame.validate();
	}
	/**
	 * 注销事件响应
	 * @param e 事件源
	 */
	protected void mimLogOffActionPerformed(ActionEvent e) {
		BMS_APP.activeUser=null;
		BMS_APP.setMenuItemAllByActiveUser();
		mainFrame.setContentPane(UserModuleFactory.getUserModuleLoginPanel(mainFrame));
		mainFrame.validate();
	}
	
}
